﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ConrtolLibrary
{

    public partial class ToggleSwitch : UserControl
    {
        private bool _on = false;
        private float _lineWidth = 1;
        private Color _switchColor1 = Color.Black;
        private Color _switchColor2 = Color.Silver;

        private Color _OnLED = Color.Lime;
        private Color _OffLED = Color.DarkGreen;
        
        
        #region Properties

        public bool On
        {
            get { return _on; }
            set { _on = value; Invalidate(); }
        }

        #endregion

        public ToggleSwitch()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            int length = Math.Min(Width, Height) - (int)(_lineWidth * 2);
            int offset = (int)(_lineWidth * 2);

            Rectangle DrawingArea = new Rectangle(offset, offset, length - offset, length - offset);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            Pen pen = new Pen(Color.DimGray, _lineWidth);
            LinearGradientBrush radBrush = new LinearGradientBrush(new Point(DrawingArea.X, DrawingArea.Y),
                new Point(DrawingArea.X + DrawingArea.Width, DrawingArea.Y + DrawingArea.Height), _switchColor2, _switchColor1);

            //draw circle
            float radius = 0.7f * DrawingArea.Width / 2;
            Rectangle circleBounds = new Rectangle((int)(DrawingArea.X - radius + DrawingArea.Width / 2),
                    (int)(DrawingArea.Y - radius + DrawingArea.Height / 2), (int)(radius * 2), (int)(radius * 2));

            e.Graphics.FillEllipse(radBrush, circleBounds);
            e.Graphics.DrawEllipse(pen, circleBounds);

            //draw rectangle
            Rectangle rectBounds; 
            if (On)
            {
                rectBounds = new Rectangle((int)(DrawingArea.X + DrawingArea.Width * 0.7 * 0.5),
                    DrawingArea.Y, (int)(DrawingArea.Width * 0.30), (int)(DrawingArea.Width * 0.85));

                e.Graphics.FillRectangle(radBrush, rectBounds);
                e.Graphics.DrawRectangle(pen, rectBounds);
            }
            else
            {
                rectBounds = new Rectangle(DrawingArea.X , (int)(DrawingArea.Y + DrawingArea.Width * 0.7f * 0.5f),
                    (int)(DrawingArea.Width * 0.85f), (int)(DrawingArea.Width * 0.30f));

                e.Graphics.FillRectangle(radBrush, rectBounds);
                e.Graphics.DrawRectangle(pen, rectBounds);
            }

           
            //draw led

            if (On)
            {
                radius = rectBounds.Width * 0.55f * 0.5f;
                e.Graphics.FillEllipse(new SolidBrush(_OnLED), rectBounds.X + rectBounds.Width / 2.0f - radius, rectBounds.Y + radius, radius * 2, radius * 2);
                e.Graphics.DrawEllipse(pen, rectBounds.X + rectBounds.Width / 2.0f - radius, rectBounds.Y + radius, radius * 2, radius * 2);
            }
            else
            {
                radius = rectBounds.Height * 0.55f * 0.5f;
                e.Graphics.FillEllipse(new SolidBrush(_OffLED), rectBounds.X + radius, rectBounds.Y + rectBounds.Height / 2.0f - radius, radius * 2, radius * 2);
                e.Graphics.DrawEllipse(pen, rectBounds.X + radius, rectBounds.Y + rectBounds.Height / 2.0f - radius, radius * 2, radius * 2);

            }
        }

        private void ToggleSwitch_Click(object sender, EventArgs e)
        {
            On = !On;
        }
    }
}
